package com.kolon.business.mngr.service.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.business.mngr.service.dao.ServiceDao;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mngr.service.controller.ServiceFileDownloadController")
public class ServiceFileDownloadController implements SimpleController {

	@Resource(name="com.kolon.business.mngr.service.dao.ServiceDaoImpl")
	private ServiceDao serviceDao;
	
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		return null;
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/business/mngr/service/iconDownload.do")
	public ModelAndView iconDownload(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Map<String, String> map = new HashMap<String, String>();
		// 파라미터
		String servNo = ComUtil.nullChk(request.getParameter("servNo"));
		// 파라미터 값 체크
		if ("".equals(servNo)) 
			return new ModelAndView("redirect:/business/mngr/service/serviceList.do");
		map.put("servNo", servNo);
		// 서비스 정보 조회
		Map servMap = serviceDao.getMap(map);
		// 서비스 정보 체크
		if (servMap == null)
			return new ModelAndView("redirect:/business/mngr/service/serviceList.do");
		// 리턴
		ModelAndView mv = new ModelAndView("downloadView");
		File downloadFile = new File(MsgUtil.getMessage("service.icon.path") 
				+ servMap.get("IMG_NM"));
		mv.addObject("downloadFile", downloadFile);
		mv.addObject("fileName", servMap.get("IMG_NM"));
		return mv;
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/business/mngr/service/servDownload.do")
	public ModelAndView servDownload(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Map<String, String> map = new HashMap<String, String>();
		ModelAndView mv = new ModelAndView("downloadView");
		// 파라미터
		String servNo = request.getParameter("servNo");
		String fileNum = request.getParameter("fileNum");
		map.put("servNo", servNo);
		// 서비스 정보 조회
		Map servMap = serviceDao.getMap(map);
		File downloadFile = new File(MsgUtil.getMessage("app.file.path") + ((String)servMap.get("FRF_TP")).toLowerCase()
				+ File.separator + servMap.get("FDR_NM") + File.separator + servMap.get("FILE_S_NM_"+fileNum));
		mv.addObject("downloadFile", downloadFile);
		mv.addObject("fileName", servMap.get("FILE_R_NM_"+fileNum));
		// 리턴
		return mv;
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/business/mngr/service/appDownload.do")
	public ModelAndView appDownload(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Map<String, String> map = new HashMap<String, String>();
		ModelAndView mv = new ModelAndView("downloadView");
		// 파라미터
		String servNo = request.getParameter("servNo");
		String appNo = request.getParameter("appNo");
		String fileNum = request.getParameter("fileNum");
		map.put("appNo", appNo);
		map.put("servNo", servNo);
		// 서비스 정보 조회
		Map servMap = serviceDao.getMap(map);
		// 서비스 앱 정보 조회
		Map appMap = serviceDao.getAppMap(map);
		File downloadFile = new File(MsgUtil.getMessage("app.file.path") + ((String)appMap.get("FRF_TP")).toLowerCase() + File.separator + servMap.get("FDR_NM") + File.separator
				+ appMap.get("FRF_VER") + File.separator + appMap.get("FILE_S_NM_"+fileNum));
		mv.addObject("downloadFile", downloadFile);
		mv.addObject("fileName", appMap.get("FILE_R_NM_"+fileNum));
		// 리턴
		return mv;
	}
	
}

